﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace ChinaSoft.Common
{
    public class Input
    {
        public static bool Check(string input, InputType inputtype)
        {
            bool flag = false;
            Regex regex;
            switch (inputtype)
            {
                case InputType.Num0:
                    regex = new Regex(@"^[0-9]*$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num1:
                    regex = new Regex(@"^\d{n}$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num2:
                    regex = new Regex(@"^\d{n,}$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num3:
                    regex = new Regex(@"^\d{m,n}$ ");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num4:
                     regex = new Regex(@"^(0|[1-9][0-9]*)$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num5:
                    regex = new Regex(@"^[0-9]+(.[0-9]{2})?$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num6:
                    regex = new Regex(@"^[0-9]+(.[0-9]{1,3})?$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num7:
                    regex = new Regex(@"^\+?[1-9][0-9]*$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num8:
                    regex = new Regex(@"^\-[1-9][0-9]*$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num9:
                    regex = new Regex(@"^\d+$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num10:
                    regex = new Regex(@"^((-\d+)|(0+))$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num11:
                    regex = new Regex(@"^.{3}$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num12:
                    regex = new Regex(@"^[A-Za-z]+$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num13:
                    regex = new Regex(@"^[A-Z]+$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num14:
                    regex = new Regex(@"^[a-z]+$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num15:
                    regex = new Regex(@"^[A-Za-z0-9]+$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num16:
                    regex = new Regex(@"^\w+$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num17:
                    regex = new Regex(@"^[a-zA-Z]\w{5,17}$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num18:
                    regex = new Regex(@"^%&',;=?$\x22]+");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num19:
                    regex = new Regex(@"^[\u4e00-\u9fa5]{0,}$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num20:
                    regex = new Regex(@"^\w+[-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num21:
                    regex = new Regex(@"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num22:
                    regex = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num23:
                    regex = new Regex(@"^\d{15}|\d{}18$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num24:
                    regex = new Regex(@"^(0?[1-9]|1[0-2])$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num25:
                    regex = new Regex(@"^((0?[1-9])|((1|2)[0-9])|30|31)$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num26:
                    regex = new Regex(@"^-?\d+$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num27:
                    regex = new Regex(@"^\d+(\.\d+)?$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num28:
                    regex = new Regex(@"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num29:
                    regex = new Regex(@"^((-\d+(\.\d+)?)|(0+(\.0+)?))$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num30:
                    regex = new Regex(@"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$");
                    flag = regex.IsMatch(input);
                    break;
                case InputType.Num31:
                    regex = new Regex(@"^(-?\d+)(\.\d+)?");
                    flag = regex.IsMatch(input);
                    break;
                default:
                    regex = new Regex("");
                    flag = regex.IsMatch(input);
                    break;
            }
            return flag;
        }
        public static bool Check(string input, int type)
        {
            bool flag = false;
            Regex regex;
            if (type == 0)
            {
                regex = new Regex(@"^[0-9]*$");
            }
            else if (type == 1)
            {
                regex = new Regex(@"^\d{n}$");
            }
            else if (type == 2)
            {
                regex = new Regex(@"^\d{n,}$");
            }
            else if (type == 3)
            {
                regex = new Regex(@"^\d{m,n}$ ");
            }
            else if (type == 4)
            {
                regex = new Regex(@"^(0|[1-9][0-9]*)$");
            }
            else if (type == 5)
            {
                regex = new Regex(@"^[0-9]+(.[0-9]{2})?$");
            }
            else if (type == 6)
            {
                regex = new Regex(@"^[0-9]+(.[0-9]{1,3})?$");
            }
            else if (type == 7)
            {
                regex = new Regex(@"^\+?[1-9][0-9]*$");
            }
            else if (type == 8)
            {
                regex = new Regex(@"^\-[1-9][0-9]*$");
            }
            else if (type == 9)
            {
                regex = new Regex(@"^\d+$");
            }
            else if (type == 10)
            {
                regex = new Regex(@"^((-\d+)|(0+))$");
            }
            else if (type == 11)
            {
                regex = new Regex(@"^.{3}$");
            }
            else if (type == 12)
            {
                regex = new Regex(@"^[A-Za-z]+$");
            }
            else if (type == 13)
            {
                regex = new Regex(@"^[A-Z]+$");
            }
            else if (type == 14)
            {
                regex = new Regex(@"^[a-z]+$");
            }
            else if (type == 15)
            {
                regex = new Regex(@"^[A-Za-z0-9]+$");
            }
            else if (type == 16)
            {
                regex = new Regex(@"^\w+$");
            }
            else if (type == 17)
            {
                regex = new Regex(@"^[a-zA-Z]\w{5,17}$");
            }
            else if (type == 18)
            {
                regex = new Regex(@"^%&',;=?$\x22]+");
            }
            else if (type == 19)
            {
                regex = new Regex(@"^[\u4e00-\u9fa5]{0,}$");
            }
            else if (type == 20)
            {
                regex = new Regex(@"^\w+[-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
            }
            else if (type == 21)
            {
                regex = new Regex(@"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$");//^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?$
            }
            else if (type == 22)
            {
                regex = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$");
            }
            else if (type == 23)
            {
                regex = new Regex(@"^\d{15}|\d{}18$");
            }
            else if (type == 24)
            {
                regex = new Regex(@"^(0?[1-9]|1[0-2])$");//正确格式为：“01”-“09”和“1”“12”
            }
            else if (type == 25)
            {
                regex = new Regex(@"^((0?[1-9])|((1|2)[0-9])|30|31)$");//正确格式为：01、09和1、31。
            }
            else if (type == 26)
            {
                regex = new Regex(@"^-?\d+$");
            }
            else if (type == 27)
            {
                regex = new Regex(@"^\d+(\.\d+)?$");
            }
            else if (type == 28)
            {
                regex = new Regex(@"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
            }
            else if (type == 29)
            {
                regex = new Regex(@"^((-\d+(\.\d+)?)|(0+(\.0+)?))$");
            }
            else if (type == 30)
            {
                regex = new Regex(@"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$");
            }
            else if (type == 31)
            {
                regex = new Regex(@"^(-?\d+)(\.\d+)?");
            }
            else
            {
                regex = new Regex("");
            }
            flag = regex.IsMatch(input);
            return flag;
        }

        public static string SetString(object o)
        {
            return Convert.ToString("" + o);
        }
        /// <summary>
        /// 转化成int类型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int SetInt(object str)
        {
            return int.Parse("0" + str);
        }
        /// <summary>
        /// 转换成decimal类型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static decimal SetDecimal(object str)
        {
            Regex RegAllNumber = new Regex("^[0-9]+[.]?[0-9]+|[0-9]+$");
            Match m = RegAllNumber.Match(SetString(str));
            if (m.Success)
                return Convert.ToDecimal(str);
            else
                return 0;
        }
        // 返回指定字符串的值
        public static string TagVal(string Tag, string TagName)
        {
            string[] strArray = Tag.Split(new char[] { ',' });
            for (int i = 0; i < strArray.Length; i++)
            {
                Regex regex = new Regex(@"(?<Keyword>\w+)\s*=\s*(?<Value>.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                for (Match match = regex.Match(strArray[i]); match.Success; match = match.NextMatch())
                {
                    if (match.Groups["Keyword"].ToString().ToLower().IndexOf(TagName.ToLower()) != -1)
                    {
                        return match.Groups["Value"].ToString().ToLower();
                    }
                }
            }
            return "";
        }
        /// <summary>
        /// 过滤字符串
        /// 作者：卢亚东
        /// 时间：2012-3-14
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string FilterStr(string source)
        {
            source = source.Replace("& ", "& ");
            source = source.Replace(" < ", "< ");
            source = source.Replace("> ", "> ");
            source = source.Replace(" ' ", " ' ' ");
            source = source.Replace("\n ", " <br/> ");
            source = source.Replace("\r\n ", " <br/> ");
            return source;
        }
        //截取函数
        public static string CutString(string str, int length, string addstr)
        {
            int i = 0, j = 0;
            foreach (char chr in str)
            {
                i += 2;
                if (i > length)
                {
                    str = str.Substring(0, j - 1);
                    if (addstr != "")
                    {
                        str += addstr;
                    }
                    break;
                }
                j++;
            }
            return str;
        }

        /// <summary>
        ///SQL注入过滤
        /// </summary>
        /// <param name="InText">要过滤的字符串</param>
        /// <returns>如果参数存在不安全字符，则返回false</returns>
        public static bool SqlFilter(string InText)
        {
            string word = "and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join";
            foreach (string i in word.Split('|'))
            {
                if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// SQL注入过滤,泛形参数
        /// </summary>
        /// <param name="strA">要过滤的泛形</param>
        /// <returns>如果参数存在不安全字符，则返回false</returns>
        public static bool SqlFilter(List<string> strA)
        {
            string word = "and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join";
            foreach (string i in word.Split('|'))
            {
                foreach (string str in strA)
                {
                    if ((str.ToLower().IndexOf(i + " ") > -1) || (str.ToLower().IndexOf(" " + i) > -1))
                    {
                        return false;
                    }
                }
            }
            return true;

        }
        /// <summary>
        /// 生成随(0至10)机号
        /// </summary>
        /// <param name="count">生成几位随机数</param>
        /// <returns></returns>
        public static string randomNum(int count)
        {
            Random rd = new Random();
            string ran = "";
            for (int i = 0; i < count; i++)
            {
                ran += rd.Next(10).ToString();
            }
            return ran;
        }
    }

    public enum InputType
    {
        /// <summary>
        /// 验证数字
        /// </summary>
        Num0 = 0,
        /// <summary>
        /// 验证n位的数字
        /// </summary>
        Num1 = 1,
        /// <summary>
        /// 验证至少n位数字
        /// </summary>
        Num2 = 2,
        /// <summary>
        /// 验证m-n位的数字
        /// </summary>
        Num3 = 3,
        /// <summary>
        /// 验证零和非零开头的数字
        /// </summary>
        Num4 = 4,
        /// <summary>
        /// 验证有两位小数的正实数
        /// </summary>
        Num5 = 5,
        /// <summary>
        /// 验证有1-3位小数的正实数
        /// </summary>
        Num6 = 6,
        /// <summary>
        /// 验证非零的正整数
        /// </summary>
        Num7 = 7,
        /// <summary>
        /// 验证非零的负整数
        /// </summary>
        Num8 = 8,
        /// <summary>
        /// 验证非负整数（正整数 + 0）
        /// </summary>
        Num9 = 9,
        /// <summary>
        /// 验证非正整数（负整数 + 0）
        /// </summary>
        Num10 = 10,
        /// <summary>
        /// 验证长度为3的字符
        /// </summary>
        Num11 = 11,
        /// <summary>
        /// 验证由26个英文字母组成的字符串
        /// </summary>
        Num12 = 12,
        /// <summary>
        /// 验证由26个大写英文字母组成的字符串
        /// </summary>
        Num13 = 13,
        /// <summary>
        /// 验证由26个小写英文字母组成的字符串
        /// </summary>
        Num14 = 14,
        /// <summary>
        /// 验证由数字和26个英文字母组成的字符串
        /// </summary>
        Num15 = 15,
        /// <summary>
        /// 验证由数字、26个英文字母或者下划线组成的字符串
        /// </summary>
        Num16 = 16,
        /// <summary>
        /// 验证用户密码 正确格式为：以字母开头，长度在6-18之间，只能包含字符、数字和下划线
        /// </summary>
        Num17 = 17,
        /// <summary>
        /// 验证是否含有 ^%&',;=?$\" 等字符
        /// </summary>
        Num18 = 18,
        /// <summary>
        /// 验证汉字
        /// </summary>
        Num19 = 19,
        /// <summary>
        /// 验证Email地址
        /// </summary>
        Num20 = 20,
        /// <summary>
        /// 验证InternetURL
        /// </summary>
        Num21 = 21,
        /// <summary>
        /// 验证电话号码
        /// </summary>
        Num22 = 22,
        /// <summary>
        /// 验证身份证号（15位或18位数字
        /// </summary>
        Num23 = 23,
        /// <summary>
        /// 验证一年的12个月
        /// </summary>
        Num24 = 24,
        /// <summary>
        /// 验证一个月的31天
        /// </summary>
        Num25 = 25,
        /// <summary>
        /// 整数
        /// </summary>
        Num26 = 26,
        /// <summary>
        /// 非负浮点数（正浮点数 + 0）
        /// </summary>
        Num27 = 27,
        /// <summary>
        /// 正浮点数
        /// </summary>
        Num28 = 28,
        /// <summary>
        /// 非正浮点数（负浮点数 + 0）
        /// </summary>
        Num29 = 29,
        /// <summary>
        /// 负浮点数
        /// </summary>
        Num30 = 30,
        /// <summary>
        /// 浮点数
        /// </summary>
        Num31 = 31
    }
}
